//
//  HomeViewController.swift
//  weibo-new
//
//  Created by mac on 2017/3/27.
//  Copyright © 2017年 gjx. All rights reserved.
//

import UIKit

class HomeViewController: UITableViewController {
    lazy var titleButton: TitleButton = TitleButton(title: "首页", imgName: "navigationbar_arrow_down", selectedImgName: "navigationbar_arrow_up")
    
    lazy var popoverAnimator: PopoverAnimator = PopoverAnimator { (isPresented: Bool) in
        self.titleButton.isSelected = isPresented
    }
    
    var isLogin: Bool = false
    
    
//    override func loadView() {
//        guard isLogin else {
//            // 加载访客视图
//            
//            return
//        }
//        
//        super.loadView()
//    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNav()
        
        // 加载数据
        loadNewData()
    }

}

// MARK:-- 设置导航栏
extension HomeViewController {
    func setupNav() {
        self.navigationItem.titleView = titleButton
        titleButton.addTarget(self, action: #selector(titleBtnClicked), for: .touchUpInside)
    }
}

// MARK:-- 事件监听
extension HomeViewController {
    @objc fileprivate func titleBtnClicked(titleButton: TitleButton) {
        titleButton.isSelected = !titleButton.isSelected
        
        // 弹出中间的view
        let popoverVC = PopoverViewController()
        popoverVC.modalPresentationStyle = UIModalPresentationStyle.custom
        popoverAnimator.presentedFrame = CGRect(x: (UIScreen.main.bounds.size.width - 217)*0.5, y: 64-10, width: 217, height: 280)
        popoverVC.transitioningDelegate = popoverAnimator
        present(popoverVC, animated: true, completion: nil)
    }
}

// MARK:-- 请求微博数据
extension HomeViewController {
    fileprivate func loadNewData() {
        let paras = ["name": "gjx"]
        JXHTTPTool.shareInstance.get(url: "http://httpbin.org/get", paras: paras, successRes: { (json: Any?) in
            if let json = json as? [String : AnyObject] { // 将json转为确定类型的字典
                print("get方式-----", json)
            }
            
        }) { (error:Error) in
            print("get方式-----", error)
        }
        
        
        JXHTTPTool.shareInstance.post(url: "http://httpbin.org/post", paras: paras, successRes: { (json: Any?) in
            if let json = json as? [String : AnyObject] { // 将json转为确定类型的字典
                print("post方式-----", json)
            }
        }) { (error:Error) in
            print("post方式-----", error)
        }
    }
}





